package system

import (
	"server/global"
	sysModel "server/model/system"
	sysRequest "server/model/system/request"
	"time"
)

var OperationLogServiceApp = new(OperationLogService)

type OperationLogService struct {
}

//获取日志列表
func (o *OperationLogService) GetList(p *sysRequest.SysOptLogGetList) ([]*sysModel.SysOperationLog, int64, error) {
	var list []*sysModel.SysOperationLog
	var total int64
	tx := global.GvaDb.Model(&sysModel.SysOperationLog{})
	if p.UserId != 0 {
		tx = tx.Where("user_id = ?", p.UserId)
	}
	if p.UserName != "" {
		tx = tx.Where("user_name like ?", "%"+p.UserName+"%")
	}
	if len(p.PermId) > 0 {
		tx = tx.Where("perm_id = ?", p.PermId[len(p.PermId)-1])
	}
	if len(p.TimeArea) > 0 {
		startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", p.TimeArea[0], time.Local)
		endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", p.TimeArea[len(p.TimeArea)-1], time.Local)
		tx = tx.Where("add_time > ? AND add_time < ?", startTime.Unix(), endTime.Unix())
	}
	err := tx.Count(&total).Order("id DESC").Limit(int(p.PageSize)).Offset(int((p.PageNo - 1) * p.PageSize)).Find(&list).Error
	//for _, log := range list {
	//	log.AddTimeStr = time.Unix(log.AddTime, 0).Format("2006-01-02 15:04:05")
	//}
	return list, total, err
}

//写入操作日志
func (o *OperationLogService) CreateSysOperationLog(SysOperationLog sysModel.SysOperationLog) (err error) {
	err = global.GvaDb.Create(&SysOperationLog).Error
	return err
}
